﻿using ArtMath.Component;
using ArtMath.Component.CoordinateSystem;
using ArtMath.Control;
using ArtMath.Core.Helper;
using ArtMath.Data;
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Reflection;
using System.Windows.Forms.Design;

namespace ArtMath.Editor
{
    [Serializable]
    public class AngleEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context?.Instance != null)
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return base.GetEditStyle(context);
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context?.Instance != null && provider != null)
            {
                IWindowsFormsEditorService es = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (es != null)
                {
                    AngleControl editor;
                    Array arr = context.Instance as Array;
                    if (arr != null)//如果是数组，说明属性是旋转角度
                    {
                        Base[] objs = new Base[arr.Length];
                        double[] angles = new double[objs.Length];
                        for (int i = 0; i < angles.Length; i++)
                        {
                            objs[i] = arr.GetValue(i) as Base;
                            angles[i] = objs[i].RotateAngle.RadValue;
                        }
                        editor = new AngleControl(angles.Same() ? angles[0] : 0);
                        editor.RefreshObject += (double newValue) =>
                        {
                            foreach (Base item in objs)
                            {
                                item.RotateAngle.SetExpression(item.Pad.ShowRad(newValue));
                            }
                        };
                        es.DropDownControl(editor);
                        foreach (Base item in objs)
                        {
                            item.RotateAngle.SetExpression(item.Pad.ShowRad(editor.NewAngle));
                        }
                    }
                    else
                    {
                        foreach (PropertyInfo item in context.Instance.GetType().GetProperties())
                        {
                            if (item.Name.Equals(context.PropertyDescriptor.Name, StringComparison.OrdinalIgnoreCase))
                            {
                                AngleStringX asx = item.GetValue(context.Instance, null) as AngleStringX;
                                bool isRayInterval = item.Name == nameof(PolarAxis.RayInterval);
                                editor = new AngleControl(asx.RadValue, isRayInterval ? Math.PI / 6 : 0);
                                editor.RefreshObject += (double newValue) =>
                                {
                                    if (isRayInterval)
                                    {
                                        PolarAxis pa = context.Instance as PolarAxis;
                                        pa.RayInterval.SetExpression(pa.Pad.ShowRad(newValue));
                                    }
                                    else
                                    {
                                        Base obj = context.Instance as Base;
                                        obj.RotateAngle.SetExpression(obj.Pad.ShowRad(newValue));
                                    }
                                };
                                es.DropDownControl(editor);
                                asx.SetExpression(asx.LinkedPad.ShowRad(editor.NewAngle));
                                return asx;
                            }
                        }
                    }
                }
            }
            return value;
        }
    }
}
